package com.deluxelx.soul.inn.web.question;

import cn.hutool.core.collection.CollectionUtil;
import com.deluxelx.soul.inn.bean.question.*;
import com.deluxelx.soul.inn.common.param.CollectedParam;
import com.deluxelx.soul.inn.common.result.SoulInnResult;
import com.deluxelx.soul.inn.service.question.QuestionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 心理问答-Controller
 *
 * @author liaozihao
 * @date 2024-04-18 15:38
 */
@RestController
@RequestMapping("/soul/inn/question")
public class QuestionController {

    @Resource
    private QuestionService questionService;

    @GetMapping("/fetchAllQuestion")
    public SoulInnResult<List<QuestionVO>> fetchAllQuestion() {
        List<QuestionVO> questionVOList = questionService.fetchQuestionList();
        if (CollectionUtil.isNotEmpty(questionVOList)) {
            return SoulInnResult.success(questionVOList);
        }
        return SoulInnResult.fail("获取心理问答列表失败");
    }

    @GetMapping("/myQuestion")
    public SoulInnResult<List<QuestionVO>> fetchMyQuestion(@RequestParam("userId") String userId) {
        List<QuestionVO> questionVOList = questionService.fetchMyQuestionList(userId);
        if (CollectionUtil.isNotEmpty(questionVOList)) {
            return SoulInnResult.success(questionVOList);
        }
        return SoulInnResult.fail("获取我的心理问答列表失败");
    }

    @GetMapping("/tagList")
    public SoulInnResult<List<Tag>> fetchTagList() {
        List<Tag> tags = questionService.fetchTagList();
        if (CollectionUtil.isNotEmpty(tags)) {
            return SoulInnResult.success(tags);
        }
        return SoulInnResult.fail("获取标签列表失败");
    }

    @PostMapping("/addMyQuestion")
    public SoulInnResult<Boolean> addMyQuestion(@RequestBody QuestionParam questionParam) {
        Boolean isSuccess = questionService.addMyQuestion(questionParam);
        if (isSuccess) {
            return SoulInnResult.success(Boolean.TRUE);
        }
        return SoulInnResult.fail("新增我的心理提问失败");
    }

    @PostMapping("/removeMyQuestion")
    public SoulInnResult<Boolean> removeMyQuestion(String questionId) {
        Boolean isSuccess = questionService.removeMyQuestion(questionId);
        if (isSuccess) {
            return SoulInnResult.success(Boolean.TRUE);
        }
        return SoulInnResult.fail("删除我的心理提问失败");
    }

    @GetMapping("/questionDetail")
    public SoulInnResult<QuestionDetailVO> fetchQuestionDetail(String questionId) {
        QuestionDetailVO questionDetailVO = questionService.fetchQuestionDetail(questionId);
        if (questionDetailVO != null) {
            return SoulInnResult.success(questionDetailVO);
        }
        return SoulInnResult.fail("获取心理问答详情失败");
    }

    @PostMapping("/addAnswer")
    public SoulInnResult<Boolean> addAnswer(@RequestBody AnswerParam answerParam) {
        Boolean isSuccess = questionService.addAnswer(answerParam);
        if (isSuccess) {
            return SoulInnResult.success(Boolean.TRUE);
        }
        return SoulInnResult.fail("新增回答失败");
    }

    @PostMapping("/removeMyAnswer")
    public SoulInnResult<Boolean> removeMyAnswer(String answerId) {
        Boolean isSuccess = questionService.removeMyAnswer(answerId);
        if (isSuccess) {
            return SoulInnResult.success(Boolean.TRUE);
        }
        return SoulInnResult.fail("删除回答失败");
    }

    @GetMapping("/answerList")
    public SoulInnResult<List<AnswerVO>> getAnswerList(@RequestParam("questionId") String questionId) {
        List<AnswerVO> answerVOS = questionService.fetchAnswerList(questionId);
        if (CollectionUtil.isNotEmpty(answerVOS)) {
            return SoulInnResult.success(answerVOS);
        }
        return SoulInnResult.fail("获取回答列表失败");
    }

    @PostMapping("/isLiked")
    public SoulInnResult<Boolean> isLiked(@RequestBody CollectedParam param) {
        try {
            Boolean isLiked = questionService.isLikeQuestion(param);
            return SoulInnResult.success(isLiked);
        } catch (Exception e) {
            return SoulInnResult.fail("获取点赞状态失败！");
        }
    }

    @PostMapping("/likeQuestion")
    public SoulInnResult<Boolean> followQuestion(@RequestBody CollectedParam param) {
        try {
            Boolean isSuccess = questionService.likeQuestion(param);
            return SoulInnResult.success(isSuccess);
        } catch (Exception e) {
            return SoulInnResult.fail("点赞失败！");
        }
    }

    @PostMapping("/unLikeQuestion")
    public SoulInnResult<Boolean> unFollowQuestion(@RequestBody CollectedParam param) {
        try {
            Boolean isSuccess = questionService.unLikeQuestion(param);
            return SoulInnResult.success(isSuccess);
        } catch (Exception e) {
            return SoulInnResult.fail("取消点赞失败！");
        }
    }

}
